
import { ajax } from 'nc-lightapp-front';
import { PAGECODE, AREA, URL, FIELDS, DATASOURCECACHE } from '../../constance';
import { getCacheDataByPk } from '../../tool/cacheDataManager';
import { showRefreshInfo } from '../../tool/messageUtil';
import { setEmptyValueDisplay } from '../viewControl/buttonControl';
import { setCardPage } from '../utils/cardPageUtil';
import { updateCacheData } from '../../tool/cacheDataManager';

export default function(props, pk, isRefresh = false) {
        // 考虑删除页时，下一页为null的情况
        if (!pk) {
                setEmptyValueDisplay(props);
                return;
        }
        // 刷新需要重新查询数据
        if (!isRefresh) {
                let cacheData = getCacheDataByPk(props, DATASOURCECACHE.dataSourceListCacheKey, pk); // 获取缓存数据
                if (cacheData) {
                        setCardPage.call(this, props, cacheData, false);
                        return;
                }
        }

        let reqData = {
                pks: [ pk ],
                pagecode: PAGECODE.cardPagecode
        };

        ajax({
                url: URL.queryCard,
                data: reqData,
                success: (res) => {
                        if (res.formulamsg && res.formulamsg instanceof Array && res.formulamsg.length > 0) {
                                props.dealFormulamsg(
                                        res.formulamsg //参数一：返回的公式对象
                                );
                        }
                        if (res.success && res.data) {
                                props.setUrlParam({ id: pk });
                                setCardPage.call(this, props, res.data, false);
                                // 更新缓存数据
                                updateCacheData(
                                        props,
                                        FIELDS.pk_head_fields,
                                        pk,
                                        res.data,
                                        AREA.cardFormId,
                                        DATASOURCECACHE.dataSourceListCacheKey
                                );
                                isRefresh ? showRefreshInfo() : '';
                        } else {
                                setEmptyValueDisplay(props);
                        }
                }
        });
}
